{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import pypinyin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[['ye'], ['ting'], ['yun']]"
      ]
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "# 中文名转拼音\n",
    "result1 = pypinyin.pinyin('叶庭云', style=pypinyin.NORMAL)\n",
    "result1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[['yè', 'xié'], ['tíng'], ['yún']]"
      ]
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "# 启用多音节来实现声调\n",
    "result2 = pypinyin.pinyin('叶庭云', heteronym=True)\n",
    "result2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'Ye Tingyun'"
      ]
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "# 因为返回的是一个嵌套的list，所以需要简单调整一下\n",
    "result_ = [i[0] for i in result1]\n",
    "result3 = result_[0].capitalize() + ' ' + ''.join(result_[1:]).capitalize()\n",
    "result3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'ZZR'"
      ]
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "# 下面是中文名转拼音首字母的方法\n",
    "result1 = pypinyin.pinyin('周芷若', style=pypinyin.NORMAL)\n",
    "result1\n",
    "result2 = ''.join([i[0][0].upper() for i in result1])\n",
    "result2"
   ]
  }
 ]
}